﻿function SmitearWebGL()
{
	this.init = function( elementId )
	{
		this.gl = null;
		this.canvas = null;
		this.viewWidth = 0;
		this.viewHeight = 0;
		this.resourceManager = null;
		this.rendererManager = null;
		this.objectManager = null;
		this.programManager = null;
		
		this.initGL( elementId );
		
		SmitearWebGL._importFile( "smitearwebgl/core/basic.js" );
		
		this.capacity = new SmitearWebGL.Core.Capacity( this.gl );
		this.capacity.checkExtension();
		
		this.initManagers();
		this.initInternalPrograms();
		this.initInternalSamplers();
		this.initRenderers();
		
		return true;
	}

	this.initGL = function( elementId )
	{
		var parent = document.getElementById( elementId );
		var glCanvas = document.createElement( "canvas" );
		glCanvas.id = "gl_canvas";
		glCanvas.width = parent.clientWidth;
		glCanvas.height = parent.clientHeight;
		
		var gl = null;
		var names = ['webgl', 'experimental-webgl', 'webkit-3d', 'moz-webgl'];
		for (var i = 0; i < names.length; i++)
		{
			try
			{
				gl = glCanvas.getContext( names[i], { "antialias" : false, "alpha" : true, "premultipliedAlpha" : true } );
				if ( gl ) break;
			}
			catch ( e )
			{
			}
		}
		if ( ! gl )
		{
			alert( "Unable to initialize WebGL. Your browser may not support it." );
			throw new Error( "Unable to initialize WebGL. Your browser may not support it." );
		}
		
		parent.appendChild( glCanvas );
	
		this.canvas = glCanvas;
		this.gl = gl;
		this.viewWidth = parent.clientWidth;
		this.viewHeight = parent.clientHeight;
		this.gl.viewport( 0, 0, parent.clientWidth, parent.clientHeight );
		this.gl.enable( this.gl.DEPTH_TEST );
		this.gl.depthFunc( this.gl.LEQUAL );
	}
	
	this.initManagers = function()
	{
		this.resourceManager = new SmitearWebGL.Core.ResourcePool( this, 128 );
		this.programManager = new SmitearWebGL.Core.ResourcePool( this, 128 );
		this.rendererManager = new SmitearWebGL.Core.ResourcePool( this, 8 );
		this.objectManager = new SmitearWebGL.Core.ResourcePool( this, 512 );
	}
	
	this.initRenderers = function()
	{
		SmitearWebGL.Renderer.InternalRenderers.createInternalRenderers( this.rendererManager );
	}
	
	this.initInternalPrograms = function()
	{
		SmitearWebGL.Programs.InternalPrograms.createInternalPrograms( this.programManager );
	}
	
	this.initInternalSamplers = function()
	{
		SmitearWebGL.Resources.InternalSamplers.createInternalSamplers( this.resourceManager );
	}
	
	this.release = function()
	{
		SmitearWebGL.Programs.InternalPrograms.releaseInternalPrograms();
		SmitearWebGL.Renderer.InternalRenderers.releaseInternalRenderers();
		SmitearWebGL.Resources.InternalSamplers.releaseInternalSamplers();
	}
}

SmitearWebGL._includeJS = function( source ) 
{ 
    if ( ( source != null ) )
    { 
        var oHead = document.getElementsByTagName( "head" ).item( 0 ); 
        var oScript = document.createElement( "script" ); 
        oScript.language = "javascript"; 
        oScript.type = "text/javascript"; 
        oScript.text = source; 
        oHead.appendChild( oScript ); 
        oHead.removeChild( oScript );
    } 
}

SmitearWebGL._importFile = function( url )
{
	var req = new XMLHttpRequest();
	if( req )
	{
		req.overrideMimeType( "text/javascript" );
		req.onreadystatechange = function()
		{
			if( this.readyState  == 4 )
			{
				if( this.status  == 200 || this.status == 0 )
				{
					SmitearWebGL._includeJS( this.responseText );
				}
			}
		}
		;
		
		if ( ! SmitearWebGL._importClassCache ) url += "?r=" + new Date().getTime();
		req.open( "GET", url, false );
		req.send( "" );
	}
}

SmitearWebGL._regNS = function( fullNS )
{
	var nsArray = fullNS.split( '.' );
	var sEval = "";
	var sNS = "";
	for ( var i = 0; i < nsArray.length; i ++ )
	{
		if ( i != 0 ) sNS += ".";
		sNS += nsArray[i];
		sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS + " = {};"
	}
	if ( sEval != "" ) eval( sEval );
}

SmitearWebGL._importClassCache = false;